package com.hy.common.excel;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.exception.ExcelAnalysisException;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hy.common.consts.CommonConst;
import com.hy.common.utils.CommonUtils;
import com.hy.common.utils.MyObjectUtils;
import com.hy.entity.pojo.HyDepartment;

import com.hy.entity.vo.HyDepartmentReadVO;

import com.hy.mapper.HyDepartmentMapper;

import com.hy.mapper.HyUnitMapper;
import com.hy.mapper.HyUserMapper;
import com.hy.service.HyDepartmentService;

import org.springframework.util.ObjectUtils;

import java.util.*;

public class HyDepartmentImport extends AbstractExcelImport<HyDepartmentReadVO, HyDepartment>{

    private HyDepartmentMapper departmentMapper;
    private HyUnitMapper unitMapper;
    private HyDepartmentService departmentService;
    private HyUserMapper userMapper;

    public HyDepartmentImport(HyDepartmentMapper departmentMapper, HyUnitMapper unitMapper, HyDepartmentService departmentService, HyUserMapper userMapper) {
        this.departmentMapper = departmentMapper;
        this.unitMapper = unitMapper;
        this.departmentService = departmentService;
        this.userMapper = userMapper;
    }

    @Override
    public void invokeHeadMap(Map<Integer, String> headMap, AnalysisContext context) {
        boolean valid = validateHeader(headMap);
        if (!valid) {
            throw new RuntimeException("表头校验失败，请检查Excel文件的列名");
        }
    }
    private boolean validateHeader(Map<Integer, String> headMap) {

        Set<String> expectedHeaders=new HashSet<>();
        Collections.addAll(expectedHeaders,"部门名称","简介","负责人","所属单位");
        Set<String> actualHeaders = new HashSet<>(headMap.values());
        return actualHeaders.containsAll(expectedHeaders);
        // 检查表头是否与预期的列名一致
    }
    @Override
    public void invoke(HyDepartmentReadVO object, AnalysisContext context) {
        try{

            if (MyObjectUtils.areAllFieldsNull(object)){
                return;
            }
            HyDepartment department=new HyDepartment();

            Integer unitId = unitMapper.findUnitIdByUnitName(object.getUnit());
            HyDepartment hyDepartment = departmentMapper.selectOne(new LambdaQueryWrapper<HyDepartment>().and(wapper ->
                    wapper.eq(HyDepartment::getDepartmentName,object.getDepartmentName())
                            .eq(HyDepartment::getUnitId, unitId)
            ));
            if (!ObjectUtils.isEmpty(hyDepartment)){
                throw new ExcelAnalysisException("部门名称重复 ：第："+(context.getCurrentRowNum()+1)+"行");
            }
            Integer managerId=MyObjectUtils.isNotBlank(object.getManagerName())?userMapper.findUserIDByUnitIdAndUserName(unitId,object.getManagerName()):CommonConst.DEFAULT_ID;
            department.setDel(1);
            department.setManagerId(managerId);
            department.setDepartmentName(object.getDepartmentName());
            department.setUnitId(unitId);
            department.setCreateDate(CommonUtils.getDate());
            department.setDepartmentDetail(object.getDepartmentDetail());
            dataList.add(department);

        }catch (Exception e){
            throw new ExcelAnalysisException(e.getMessage());
        }
    }

    @Override
    protected void doSaveData(List<HyDepartment> data) {
        departmentService.saveStuListWithBatch(data);
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {
        doSaveData(dataList);
    }
}
